<?php
/**
 * Convert a persons name to Title Case
 *
 * This name converter applis Title Case to peoples
 * names, with special rules for certain types of names.
 *
 * For example: Lee-Anderson, O'Reilly,
 *              McArthur, MacArthur, FitzGerald
 *
 * @link http://nz2.php.net/ucwords
 *
 * @param string $str The name to convert
 * @return string     The converted name
 */
function titleCaseName($name)
{
    # (?: ^ | \\b )        assertion: beginning of string or a word boundary
    # ( O\' | Ma?c | Fitz) attempt to match Irish surnames
    # ( [^\W\d_] )         match next char; we exclude digits and _ from \w
    $pattern = '/(?:^|\\b)(O\\\'|Ma?c|Fitz)([^\\W\\d_])/xe';
    $replacement = "'\$1' . strtoupper('\$2')";
    $name = ucwords(strtolower($name));
    return preg_replace($pattern, $replacement, $name);
}
?>

